
const createRecycleContext = require('miniprogram-recycle-view');
const Request = require("../../utils/util");//导入模块
let ctx;
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    recycleHeight: wx.getSystemInfoSync().windowHeight - 86, //初始化滚动列表的高度
    listLoadingShow: false,

    source: ['全部', '自然到店', '小区地推', '电话营销', '朋友介绍', '装修公司', '广告推广', '邀约到店', '渠道'],
    state: ['全部', '意向', '待见面', '见面', '定金', '已量房', '不及时', '合同', '流失'],
    customerLevel: ['全部', '中意向', '高意向', '准意向'],
    parameter: {
      pageNum: 1,
      pageSize: 10,
      name: '',
      source: 0,
      state: 0,
      customerLevel: 0,
    }
  },


  /**
   * 初始化 RecycleList 插件
   */
  initRecycleList: function () {
    ctx = createRecycleContext({
      id: 'recycleId',
      dataKey: 'recycleList',
      page: this,
      itemSize: this.itemSizeFunc(),
    })
  },
  itemSizeFunc: function (item, idx) {
    return {
      width: wx.getSystemInfoSync().windowWidth,
      height: 80
    }
  },
  /**
   * 获取页面数据
   */
  getListData: function (isPageDown) {
    wx.showLoading({ title: '读取中', mask: true, })

    let p = {
      pageNum: this.data.parameter.pageNum,
      pageSize: this.data.parameter.pageSize,
      name: this.data.parameter.name,
      source: this.data.parameter.source,
      state: this.data.parameter.state,
      customerLevel: this.data.parameter.customerLevel
    }

    p.source = p.source == "0" ? "" : p.source;
    p.state = p.state == "0" ? "" : p.state;
    p.customerLevel = p.customerLevel == "0" ? "" : p.customerLevel;

    Request.get("emCustomer/getCustomerList", p).then(res => {
      this.setData({ listLoadingShow: false })
      wx.hideLoading();
      if (!isPageDown) {
        ctx.splice(0, ctx.getList().length)
      }
      ctx.append(res.list);
    }).catch(err => {
      wx.showToast({
        title: err.data.return_msg,
        icon: 'none',
        duration: 1000
      })
    })
  },

  /**
   * 点击搜索 数据清零 
   */
  bindconfirm: function (e) {
    let pageNum = "parameter.pageNum";
    let source = "parameter.source";
    let state = "parameter.state";
    let customerLevel = "parameter.customerLevel";
    this.setData({
      [pageNum]: 0,
      [source]: 0,
      [state]: 0,
      [customerLevel]: 0
    })
    this.getListData(false);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initRecycleList();
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   *  跳转到详情页面
   * @param {} event 
   */
  queryDetails: function (event) {
    app.parameter = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/details/details',
    })
  },

  /**
   * 滚动条滚动到底部
   */
  scrolltolower: function () {
    this.setData({
      listLoadingShow: true
    })
    let pageNum = "parameter.pageNum";
    this.setData({ [pageNum]: this.data.parameter.pageNum + 1 })
    this.getListData(true);
  },


  /**
   * 
   * 绑定来源pick 组件值
   */
  bindSourceChange: function (e) {
    let pageNum = "parameter.pageNum";
    let name = "parameter.name";
    let state = "parameter.state";
    let customerLevel = "parameter.customerLevel";
    let source = "parameter.source";

    this.setData({
      [source]: e.detail.value,
      [pageNum]: 0,
      [state]: 0,
      [customerLevel]: 0,
      [name]: ''
    })
    this.getListData(false);

  },
  /**
   * 
   * 绑定状态pick 组件值
   */
  bindStateChange: function (e) {

    let pageNum = "parameter.pageNum";
    let name = "parameter.name";
    let state = "parameter.state";
    let customerLevel = "parameter.customerLevel";
    let source = "parameter.source";

    this.setData({
      [state]: e.detail.value,
      [source]: 0,
      [pageNum]: 0,
      [customerLevel]: 0,
      [name]: ''
    })

    this.getListData(false);
  },

  /**
   * 绑定级别 pick值
   */
  bindLevelChange: function (e) {

    let pageNum = "parameter.pageNum";
    let name = "parameter.name";
    let state = "parameter.state";
    let customerLevel = "parameter.customerLevel";
    let source = "parameter.source";

    this.setData({
      [state]: 0,
      [source]: 0,
      [pageNum]: 0,
      [customerLevel]: e.detail.value,
      [name]: ''
    })
    this.getListData(false);
  },
  /**
   * 绑定输入搜索
   */
  bindinput: function (e) {
    let name = "parameter.name";
    this.setData({
      [name]: e.detail.value
    })
  },

  /**
 * 录入入口
 */
  enter: function () {
    wx.navigateTo({
      url: '/pages/enter/enter',
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getListData(false);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})